package pt.ipleiria.plp;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

/**
 * Class for images stored on the phone
 * @author Filipe Felisberto e Ricardo Silva
 */
public class ImagePath extends Image {

    private String path;

    public ImagePath(String path, String title, String description, String mediaType) {
        super(title, description, mediaType);

        this.path = path;

    }

    /**
     * @return the path
     */
    public String getPath() {
        return path;
    }
    
    public byte[] getImageByte() throws Exception {

        byte[] imaData = null;
        FileConnection fconn = (FileConnection) Connector.open(getPath());
        if (!fconn.exists()) {
            System.out.println("BUG!");
        } else {
            InputStream is = fconn.openInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int byt = 0;
            do {
                byt = is.read();
                if (byt != -1) {
                    baos.write(byt);
                }
            } while (byt != -1);
            imaData = baos.toByteArray();
        }
        fconn.close();

        return imaData;

    }
}
